<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/8 0008
 * Time: 上午 11:57
 */

namespace app\sports_admin\service;

use app\exception\DeviceTemporaryNumException;
use app\exception\ExpensesReceiptsException;
use app\sports_admin\model\DeviceTemporaryNum as DeviceTemporaryNumModel;
use app\sports_admin\model\Equipment as EquipmentModel;
use think\Db;

class DeviceTemporaryNum extends Base
{
    /**
     * 临时编号列表
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $number 设备编号
     */
    public static function numberList()
    {
        $params = self::getHttpRequestPrograms();
        $params = $params->param();
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 1;
        $number = trim($params['searchName']);
        $batch = intval($params['batch']);
        $version = intval($params['version']);
        return DeviceTemporaryNumModel::numberList($page,$rows,$number,$batch,$version);
    }
    /**
     * 生成临时编号   型号+版本号+年份+批次+序列号
     */
    public static function insertTemporaryNumber()
    {
        $params = self::getHttpRequestPrograms();
        $params = $params->param();
        //型号
        $version = sprintf("%02d",$params['version']);
        //版本号
        $versionNum = sprintf("%02d",$params['version_number']);
        //年份
        $year = date('y');
        //批次
        $batch = sprintf("%03d",$params['batch']);
        // num 生成个数
        for($i=1;$i<=intval($params['num']);$i++) {
            $number = sprintf("%06d", $i);
            $array[$i]['number'] = $version . $versionNum . $year . $batch . $number;
            $array[$i]['num'] = $number;
            $array[$i]['version'] = $params['version'];
            $array[$i]['version_number'] = $params['version_number'];
            $array[$i]['year'] = $year;
            $array[$i]['batch'] = $params['batch'];
        }
        Db::startTrans();
        $bool = (new DeviceTemporaryNumModel())->saveAll($array);
        if(count($bool) == 0){
            Db::rollback();
            throw new DeviceTemporaryNumException(['msg'=>'生成失败','errorCode'=>400]);
        }
        Db::commit();
        return json(['code'=>200,'msg'=>'生成成功']);
    }
    /**
     * 删除设备编号
     * @param array $id id号
     */
    public static function delete()
    {
        $params = self::getHttpRequestPrograms();
        $info = $params->param();
        $id = implode(',',$info['id']);
        $bool = DeviceTemporaryNumModel::destroy($id);
        if(!$bool){
            throw new DeviceTemporaryNumException(['msg'=>'删除失败','errorCode'=>400]);
        }
        return json(['msg'=>'删除成功','code'=>200]);
    }
    /**
     * 弃用设备编号
     * @param array $id id号
     */
    public static function abandoning()
    {
        $params = self::getHttpRequestPrograms();
        $info = $params->param();
        $bool = (new DeviceTemporaryNumModel())->save(['status'=>3],function ($query)use($info){
            $query->whereIn('id',$info['id']);
        });
        if(!$bool){
            throw new DeviceTemporaryNumException(['msg'=>'删除失败','errorCode'=>400]);
        }
        return json(['msg'=>'弃用成功','code'=>200]);
    }
    /**
     * 导出excel 文件
     */
    public static function exportInfo()
    {
        $params = self::getHttpRequestPrograms();
        $info = $params->param();
        //excel文件名
        $name = "设备编号";
        //表头名称
        $header = ['设备编号信息'];
        $key = 0;
        $data = DeviceTemporaryNumModel::where('batch',$info['batch'])->where('status',1)->select();
        if(!$data){
            throw new ExpensesReceiptsException(['msg'=>'无效的设备号','errorCode'=>400]);
        }
		foreach($data as $val){
			$array[$key]['number'] = $val->number;
            $key++;
		}
        excelExport($name,$header,$array);
    }
    /**
     * 生成正式编号
     */
    public static function officialNumber()
    {
        $temArray = [];
        $params = self::getHttpRequestPrograms();
        $info = $params->param();
        $data = DeviceTemporaryNumModel::where('batch',$info['batch'])->where('status',1)->select();
        if(!$data){
            throw new ExpensesReceiptsException(['msg'=>'无效的设备号','errorCode'=>400]);
        }
        foreach($data as $val){
            $arr['number'] = $val->number;
            $arr['device_batch'] = $val->batch;
            $temArray[] = $arr;
        }

        Db::startTrans();
        $bool = (new EquipmentModel())->saveAll($temArray);
        if(count($bool) == 0){
            Db::rollback();
            throw new DeviceTemporaryNumException(['msg'=>'生成失败','errorCode'=>400]);
        }
        Db::commit();
        return json(['code'=>200,'msg'=>'生成成功']);
    }
}